package com.weiyuanstudio.nextstep.controller.database;

import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.weiyuanstudio.nextstep.service.database.BaselineService;
import jakarta.servlet.http.HttpServletRequest;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;

@RestController
@RequestMapping("/api/database")
public class BaselineController {
    @Autowired
    private BaselineService baselineService;

    @PostMapping("/upload-baseline")
    public String uploadBaseline(@RequestParam("file") MultipartFile file, HttpServletRequest request) throws IOException {
        String characterEncoding = request.getCharacterEncoding();
        byte[] bytes = file.getBytes();
        String json = new String(bytes, characterEncoding); // 上传的基线数据
        baselineService.saveBaselineJson(json);

        return String.valueOf(bytes.length);
    }
}
